package com.anda.logistics.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;

import com.anda.logistics.map.util.JsonStatus;
import com.anda.logistics.model.SystemMenu;
import com.anda.logistics.model.SystemMenuRights;
import com.anda.logistics.service.SystemMenuService;
import com.anda.logistics.service.SystemRightsService;
import com.anda.logistics.util.JQGridJsonData;
import com.anda.logistics.util.JQGridPager;

@ParentPackage("json-default")
public class SystemRightsAction extends AndaBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2776567000574167646L;
	private SystemMenuService systemMenuService;
	private SystemRightsService systemRightsService;
	private List<SystemMenu> menuList; 
	private JQGridPager pager;
	private JQGridJsonData results;
	private JsonStatus status;
	private SystemMenuRights smr ;
	
	@Action(value="/manager/rights/system_menu_rights"
			,results=@Result(name="success", location="system-menu-rights.jsp")
	)
	public String menuRights(){
		menuList = systemMenuService.getAll();
		return SUCCESS;
	}
	
	@Action(value="/manager/rights/menu-rights-list"
		,results=@Result(name="success",type="json",params={"root","results","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String menuRightsList(){
		//System.out.println(pager);
		List<SystemMenuRights> list = systemRightsService.getPagerList(pager);
		//System.out.println(pager);
		long records = systemRightsService.getPagerRecords(pager);
		long total = records / pager.getRows();
		if ( records % pager.getRows() > 0) total ++;
		results = new JQGridJsonData();
		results.setPage(pager.getPage());
		results.setRecords(records);
		results.setTotal(total);
		results.setRowsFromList(list,"id",new String[]{"id","menuId","menuId","rights01","rights02","rights03","rights04","rights05","rights06","rights07","rights08","rights09","rights10","rights11","rights12","rights13","rights14","rights15","rights16","rights17","rights18","rights19","rights20"});
		return SUCCESS;
	}
	
	@Action(value="/manager/rights/menu-rights-edit"
		,results=@Result(type="json",name="success",params={"root","status","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String menuRightsEdit(){
		status = new JsonStatus(true);
		try {
			if (pager.getOper().equals("add")){
				systemRightsService.save(smr);
			} else if (pager.getOper().equals("edit")){
				smr.setId(Integer.parseInt(pager.getId()));
				systemRightsService.update(smr);
			} else if (pager.getOper().equals("del")){
				systemRightsService.deleteAll(pager.getId().split(","));
			}
		} catch (Exception e){
			e.printStackTrace();
			status.setMsg(e.getMessage());
			status.setSc(false);
		}
	
		return SUCCESS;
	}
	
	@Resource
	public void setSystemMenuService(SystemMenuService systemMenuService){
		this.systemMenuService = systemMenuService;
	}
	@Resource
	public void setSystemRightsService(SystemRightsService systemRightsService){
		this.systemRightsService = systemRightsService;
	}
	@JSON(serialize=false)
	public List<SystemMenu> getMenuList(){
		return menuList;
	}
	public void setPager(JQGridPager pager){
		this.pager = pager;
	}
	public JQGridPager getPager(){
		return pager;
	}
	public JQGridJsonData getResults(){
		return results;
	}
	public JsonStatus getStatus(){
		return status;
	}
	
	public void setSmr(SystemMenuRights smr){
		this.smr = smr;
	}
	@JSON(serialize=false)
	public SystemMenuRights getSmr(){
		return smr;
	}
}
